package cn.com.majiang.service.impl;

import cn.com.majiang.dao.TechargerecordDao;
import cn.com.majiang.domain.Techargerecord;
import cn.com.majiang.model.TechargeModel;
import cn.com.majiang.param.TechargeParam;
import cn.com.majiang.service.ITechargerecordService;
import cn.com.majiang.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jiangyanxin on 2017/5/29.
 */
@Service
public class TechargerecordServiceImpl implements ITechargerecordService{
    Logger logger = LoggerFactory.getLogger(TechargerecordServiceImpl.class);
    @Autowired
    TechargerecordDao techargerecordDao;

    @Override
    public Map<String, Object> searchRechargeList(TechargeParam param) {
        Map totalmap = techargerecordDao.selectTechargeCount(param);
        if(param.getPage() != null) {
            param.setStartNum(param.getLimit() * (param.getPage() - 1));
        }
        List<Techargerecord> list = techargerecordDao.selectTechargeList(param);
        List<TechargeModel> modelList = new ArrayList<>();
        if(list != null && list.size() > 0){
            for(Techargerecord techargerecord : list){
                TechargeModel model = new TechargeModel();
                model.setCreatestr(DateUtils.dateTimeToStr(techargerecord.getCreatetime()));
                model.setMark(techargerecord.getMark());
                model.setTechargemoney(techargerecord.getTechargemoney()+"");
                if(techargerecord.getAccountId()!= null){
                    model.setUserid(techargerecord.getAccountId()+"");
                }
                if(techargerecord.getManagerId()!= null){
                    model.setUserid(techargerecord.getManagerId()+"");
                }
                model.setType(techargerecord.getType());
                model.setManagerid(techargerecord.getManagerUpId()+"");
                modelList.add(model);
            }
        }
        Map map = new HashMap();
        map.put("rows",modelList);
        map.put("count",totalmap.get("total"));
        map.put("sumcard",totalmap.get("summoney"));
        return map;
    }

    @Override
    public Boolean saveRoomCardRecord(Techargerecord techargerecord) {
        if(techargerecord != null){
            return techargerecordDao.save(techargerecord) >0 ;
        }else{
            return false;
        }
    }

    public Boolean saveManagerTechargerecord(int managerId,String chargerId,int playcardNum,String chargetype){
        Techargerecord techargerecord = new Techargerecord();
        if(playcardNum > 0 ){
            techargerecord.setMark("充值");
        }
        techargerecord.setManagerId(managerId);
        techargerecord.setManagerUpId(chargerId);
        techargerecord.setTechargemoney(playcardNum);
        techargerecord.setType(chargetype);
        return saveRoomCardRecord(techargerecord);
    }

    public Boolean saveAccountTechargerecord(int accountId,String chargerId,int playcardNum,String chargetype){
        Techargerecord techargerecord = new Techargerecord();
        if(playcardNum > 0 ){
            techargerecord.setMark("充值");
        }
        techargerecord.setAccountId(accountId);
        techargerecord.setManagerUpId(chargerId);
        techargerecord.setTechargemoney(playcardNum);
        techargerecord.setType(chargetype);
        return saveRoomCardRecord(techargerecord);
    }

    @Override
    public Map getTotalCard(String managerId) {
        int sellnum = techargerecordDao.getSellCardNum(managerId);
        int buynum = techargerecordDao.getBuyCardNum(managerId);
        logger.info("sellnum"+sellnum);
        logger.info("buynum"+buynum);
        Map map = new HashMap();
        map.put("sellnum",sellnum);
        map.put("buynum",buynum);
        return map;
    }
}
